The input std::istrstream
, output std::ostrstream
, and combined std::iostream
stream classes are deprecated
since their introduction in the C++98 standard.
Those stream classes support two use cases, and their behavior differs vastly depending on how the object is constructed:
- When constructed without argument, the stream object manages a dynamically allocated buffer;
- When constructed from
char*
buffer and size, the stream object uses only the provided buffer.
The above behaviors make the strstream
classes hard to use correctly. As illustration the str()
member function:
- returns a buffer that has unclear ownership;
- does not return the size of readable characters nor guarantees null-termination of the buffer;
- may leak unless
freeze(false)
is called afterwards.
With the introduction of the std::spanstream
, std::ispanstream
and std::ospanstream
C++23, all uses of the
std::strstream
, std::ostrstream
and std::ispanstream
classes may be replaced, with one of the following
classes:
-
std::stringstream
and std::ostringstream
to use managed and growing buffer,
-
std::spanstream
, std::ospanstream
and std::ispanstream
to use preallocated buffer with fixed size.
This rule will raise an issue when the new object of strstream
is created:
std::string printData(std::string_view entry, int count) {
std::ostrstream os; // Noncompliant
os << "The entry '" << entry << "' was repeated " << count << " times." << std::ends;
char const* content = os.str();
os.freeze(false);
return std::string(content);
}
int readInt(std::istrstream);
void process() {
readInt(std::istrstream("10")); // Noncompliant
}
Furthermore, the issue will be raised if a data member of the class is declared with one of strstream
types:
class Printer {
/* .... */
private:
std::ostrstream os; // Noncompliant
};